package com.viish.apps.tripletriad.views;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.Paint.Align;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;

public class CompleteCardView extends ImageView
{	
	private static final long serialVersionUID = 1L;
	private Bitmap picture1, picture2;
	private Bitmap back;
	private boolean visible;
	private int viewColor, trueColor, alternativeColor, taillePinceau;
	private int posx = 0, posy = 0;
	private String top = "", left = "", bot = "", right = "";
	private Paint p;
	private final float shadowRadius = 1.0f;
    private final int shadowOffset = 1;
    private final int shadowColor = 0xffffffff;
    private int hasBeenModifiedByElement = 0;
	
	public CompleteCardView(Context context, Bitmap picture1, Bitmap picture2, Bitmap back, String top, String left, String bot, String right) //, int resizeX, int resizeY) 
	{
		super(context);
		
		this.picture1 = picture1; //resizePicture(resizeX, resizeY, picture1);
		this.picture2 = picture2; //resizePicture(resizeX, resizeY, picture2);
		this.back = back; //resizePicture(resizeX, resizeY, back);
		this.visible = true;
		this.setClickable(false); 
		
		this.viewColor = trueColor = 1;
		this.top = top;
		this.left = left;
		this.bot = bot;
		this.right = right;
		if (this.top == "10") { this.top = "A" ; }
		if (this.left == "10") { this.left = "A" ; }
		if (this.bot == "10") { this.bot = "A" ; }
		if (this.right == "10") { this.right = "A" ; }
		
		this.p = new Paint(Paint.ANTI_ALIAS_FLAG);
		Typeface typeface = Typeface.createFromAsset(context.getAssets(), "font.ttf");
		this.p.setTypeface(typeface); 
		this.p.setColor(0xff000000);
		this.p.setTextAlign(Align.LEFT);
		this.p.setTextSize(15);
		this.taillePinceau = 15;
	}
	
	public void resetElement()
	{
		if (hasBeenModifiedByElement == 1)
			malusElementaire();
		else if (hasBeenModifiedByElement == -1)
			bonusElementaire();
	}
	
	public void bonusElementaire()
	{
		hasBeenModifiedByElement = 1;
		if (!this.top.equals("A"))
		{
			int temp = Integer.parseInt(this.top);
			temp += 1;
			if (temp >= 10) this.top = "A";
			else this.top = temp + "";
		}
		
		if (!this.left.equals("A"))
		{
			int temp = Integer.parseInt(this.left);
			temp += 1;
			if (temp >= 10) this.left = "A";
			else this.left = temp + "";
		}
		
		if (!this.bot.equals("A"))
		{
			int temp = Integer.parseInt(this.bot);
			temp += 1;
			if (temp >= 10) this.bot = "A";
			else this.bot = temp + "";
		}
		
		if (!this.right.equals("A"))
		{
			int temp = Integer.parseInt(this.right);
			temp += 1;
			if (temp >= 10) this.right = "A";
			else this.right = temp + "";
		}
		
		this.invalidate();
	}
	
	public void malusElementaire()
	{
		hasBeenModifiedByElement = -1;
		if (!this.top.equals("A"))
		{
			int temp = Integer.parseInt(this.top);
			temp -= 1;
			this.top = temp + "";
		}
		else
		{
			this.top = "9";
		}
		
		if (!this.left.equals("A"))
		{
			int temp = Integer.parseInt(this.left);
			temp -= 1;
			this.left = temp + "";
		}
		else
		{
			this.left = "9";
		}
		
		if (!this.bot.equals("A"))
		{
			int temp = Integer.parseInt(this.bot);
			temp -= 1;
			this.bot = temp + "";
		}
		else
		{
			this.bot = "9";
		}
		
		if (!this.right.equals("A"))
		{
			int temp = Integer.parseInt(this.right);
			temp -= 1;
			this.right = temp + "";
		}
		else
		{
			this.right = "9";
		}
		
		this.invalidate();
	}
	
	public void setColor(int color)
	{
		this.viewColor = this.trueColor = color;
	}
	
	public void swapColor()
	{
		if (this.viewColor == 1)
		{
			this.trueColor = 2;
			applyRotation(0, 90);
		}
		else
		{
			this.trueColor = 1;
			applyRotation(0, -90);
		}
	}
	
	public void setAlternativeColor(int alt)
	{
		this.alternativeColor = alt;
	}
	
	public int getAlternativeColor()
	{
		return this.alternativeColor;
	}
	
	public int getColor()
	{
		return this.trueColor;
	}
	
	public void flipCard()
	{
		this.visible = ! this.visible;
		
		this.invalidate();
	}
	
	public void move(int x, int y)
	{
		this.posx = x;
		this.posy = y;
			
		this.invalidate();
	}
	
	public boolean isFaceUp()
	{
		return this.visible;
	}
	
	public Bitmap getBitmap()
	{
		if (this.viewColor == 1) return this.picture1;
		else if (this.viewColor == 2) return this.picture2;
		else return null;
	}
	
	public int getPositionX()
	{
		return this.posx;
	}
	
	public int getPositionY()
	{
		return this.posy;
	}
	
	public int getRealWidth()
	{
		return this.picture1.getWidth();
	}
	
	public int getRealHeight()
	{
		return this.picture1.getHeight();
	}
	
	@Override
	protected void onDraw(Canvas canvas)
	{
		if (this.visible)
		{
			if (this.viewColor == 1) canvas.drawBitmap(this.picture1, this.posx, this.posy, null); // Relatif a la taille de la zone de dessin
			else if (this.viewColor == 2) canvas.drawBitmap(this.picture2, this.posx, this.posy, null);
			
			this.p.setShadowLayer(shadowRadius, shadowOffset, shadowOffset, shadowColor);
			canvas.drawText(this.top, posx + (3 * taillePinceau / 2 + 2) / 2, posy + taillePinceau, this.p);
			this.p.setShadowLayer(shadowRadius, shadowOffset, -shadowOffset, shadowColor);
			canvas.drawText(this.top, posx + (3 * taillePinceau / 2 + 2) / 2, posy + taillePinceau, this.p);
			this.p.setShadowLayer(shadowRadius, -shadowOffset, shadowOffset, shadowColor);
			canvas.drawText(this.top, posx + (3 * taillePinceau / 2 + 2) / 2, posy + taillePinceau, this.p);
			this.p.setShadowLayer(shadowRadius, -shadowOffset, -shadowOffset, shadowColor);
			canvas.drawText(this.top, posx + (3 * taillePinceau / 2 + 2) / 2, posy + taillePinceau, this.p);
			
			this.p.setShadowLayer(shadowRadius, shadowOffset, shadowOffset, shadowColor);
			canvas.drawText(this.left, posx + 3, posy + 3 * taillePinceau / 2, this.p);
			this.p.setShadowLayer(shadowRadius, shadowOffset, -shadowOffset, shadowColor);
			canvas.drawText(this.left, posx + 3, posy + 3 * taillePinceau / 2, this.p);
			this.p.setShadowLayer(shadowRadius, -shadowOffset, shadowOffset, shadowColor);
			canvas.drawText(this.left, posx + 3, posy + 3 * taillePinceau / 2, this.p);
			this.p.setShadowLayer(shadowRadius, -shadowOffset, -shadowOffset, shadowColor);
			canvas.drawText(this.left, posx + 3, posy + 3 * taillePinceau / 2, this.p);
			
			this.p.setShadowLayer(shadowRadius, shadowOffset, shadowOffset, shadowColor);
			canvas.drawText(this.bot, posx + (3 * taillePinceau / 2 + 2) / 2, posy + 2 * taillePinceau, this.p);
			this.p.setShadowLayer(shadowRadius, shadowOffset, -shadowOffset, shadowColor);
			canvas.drawText(this.bot, posx + (3 * taillePinceau / 2 + 2) / 2, posy + 2 * taillePinceau, this.p);
			this.p.setShadowLayer(shadowRadius, -shadowOffset, shadowOffset, shadowColor);
			canvas.drawText(this.bot, posx + (3 * taillePinceau / 2 + 2) / 2, posy + 2 * taillePinceau, this.p);
			this.p.setShadowLayer(shadowRadius, -shadowOffset, -shadowOffset, shadowColor);
			canvas.drawText(this.bot, posx + (3 * taillePinceau / 2 + 2) / 2, posy + 2 * taillePinceau, this.p);
			
			this.p.setShadowLayer(shadowRadius, shadowOffset, shadowOffset, shadowColor);
			canvas.drawText(this.right, posx + (3 * taillePinceau / 2) - 2, posy + 3 * taillePinceau / 2, this.p);
			this.p.setShadowLayer(shadowRadius, shadowOffset, -shadowOffset, shadowColor);
			canvas.drawText(this.right, posx + (3 * taillePinceau / 2) - 2, posy + 3 * taillePinceau / 2, this.p);
			this.p.setShadowLayer(shadowRadius, -shadowOffset, shadowOffset, shadowColor);
			canvas.drawText(this.right, posx + (3 * taillePinceau / 2) - 2, posy + 3 * taillePinceau / 2, this.p);
			this.p.setShadowLayer(shadowRadius, -shadowOffset, -shadowOffset, shadowColor);
			canvas.drawText(this.right, posx + (3 * taillePinceau / 2) - 2, posy + 3 * taillePinceau / 2, this.p);
		}
		else
		{
			canvas.drawBitmap(this.back, this.posx, this.posy, null);
		}
	}
	
	public void resizePictures(int x, int y)
	{
		picture1 = resize(picture1, x, y);
		picture2 = resize(picture2, x, y);
		back = resize(back, x, y);
		
		this.taillePinceau = y / 5;
		this.p.setTextSize(this.taillePinceau);
	}
	
	private Bitmap resize(Bitmap bm, int x, int y)
	{
		int width = bm.getWidth();
		int height = bm.getHeight();
		int newWidth = x;
		int newHeight = y;
		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;
		
		Matrix matrix = new Matrix();
		matrix.postScale(scaleWidth, scaleHeight);
		Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);

		return resizedBitmap;
	}
	
	private void applyRotation(float start, float end) // Rotation 3D de l'image, avec changement de couleur au milieu !
	{
		final CompleteCardView cp = this;
		
		final float centerX = posx + (picture1.getWidth() / 2.0f);
		final float centerY = posy + (picture1.getHeight() / 2.0f);

		final FlipCardAnimation rotation = new FlipCardAnimation(start, end, centerX, centerY);
		rotation.setDuration(300);
		rotation.setFillAfter(true);
		rotation.setInterpolator(new AccelerateInterpolator());
		rotation.setRepeatMode(Animation.REVERSE);
		rotation.setRepeatCount(1);
		rotation.setAnimationListener(new AnimationListener() 
		{
			public void onAnimationStart(Animation animation) 
			{
				
			}
			public void onAnimationRepeat(Animation animation) 
			{
				cp.viewColor = cp.trueColor;
				cp.invalidate();
			}
			public void onAnimationEnd(Animation animation)
			{
				
			}
		});
		
		startAnimation(rotation);
	}
}
